package com.taofuxn.yycreazy.adpater;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.adpater.base.TaofuRecyclerAdapter;
import com.taofuxn.yycreazy.adpater.base.TaofuRecyclerViewHodler;
import com.taofuxn.yycreazy.database.model.Collection;
import com.taofuxn.yycreazy.ports.ITaofuDeleListener;
import com.taofuxn.yycreazy.ui.activity.DisplayImgActivity;
import com.taofuxn.yycreazy.util.MeasureUtils;

import java.util.ArrayList;

/**
 * 收藏夹
 * Created by taofuxn on 2016/9/20.
 */
public class CollectionAdapter extends TaofuRecyclerAdapter<Collection> {

    private ITaofuDeleListener mITaofuDeleListener;
    public CollectionAdapter(int layoutId, ArrayList<Collection> mDatas) {
        super(layoutId, mDatas);
    }

    @Override
    protected void convert(final TaofuRecyclerViewHodler holder, final Collection b) {

        if (!TextUtils.isEmpty(b.getContent()))
            holder.setText(R.id.contentCollet, b.getContent());

        if (!TextUtils.isEmpty(b.getUrl())) {
            holder.retrieveView(R.id.colletImg).setVisibility(View.VISIBLE);
            Glide.with(holder.itemView.getContext()).load(b.getUrl())
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into((ImageView) holder.retrieveView(R.id.colletImg));
            holder.retrieveView(R.id.colletImg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(holder.itemView.getContext(), DisplayImgActivity.class);
                    intent.putExtra("url", b.getUrl());
                    holder.itemView.getContext().startActivity(intent);
                }
            });
        } else {
            holder.retrieveView(R.id.colletImg).setVisibility(View.GONE);
        }

        holder.retrieveView(R.id.itemMenu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectPopWin(holder.itemView.getContext(),v,holder.getAdapterPosition());
            }
        });
    }

    private void showSelectPopWin(Context context, View v, final int position) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(v.getContext()).inflate(
                R.layout.pop_window_item_menu, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        contentView.findViewById(R.id.menu_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                if (mITaofuDeleListener != null)
                    mITaofuDeleListener.deleteItem(position);
            }
        });

        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                android.R.drawable.screen_background_dark_transparent));
        popupWindow.setAnimationStyle(R.style.popwin_itemmenu_show);
        popupWindow.showAsDropDown(v,-MeasureUtils.dipToPix(80,context),0);
    }


    public void setmITaofuDeleListener(ITaofuDeleListener mITaofuDeleListener){
        this.mITaofuDeleListener = mITaofuDeleListener;
    }

}
